//
//  SettingTableViewController.m
//  CarTown
//
//  Created by xionghao on 15/8/17.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "SettingTableViewController.h"
#import "SettingItem.h"
#import "MessageDetailTableViewController.h"
#import "ShareView.h"
#import "WebViewController.h"
#import "AboutViewController.h"
#import "MYUpatePassViewController.h"
#import "UMSocial.h"
#import "Puser.h"
#import "AppDelegate.h"
@interface SettingTableViewController ()<ShareclickDelegate,UIAlertViewDelegate>
@property (nonatomic,strong) NSArray *settingitemarray;
@property (nonatomic,strong) UIView *footview;
@property (nonatomic,strong) Puser *user;
@end

@implementation SettingTableViewController
+(instancetype)setting{
    SettingTableViewController *setting=[[SettingTableViewController alloc]initWithStyle:UITableViewStylePlain];
    setting.hidesBottomBarWhenPushed=YES;
    return setting;
}
-(Puser *)user{
    if (_user==nil) {
        _user=[Puser getUser];
    }
    return _user;
}

-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    self.navigationItem.title=@"设置";
    self.tableView.backgroundColor=UIColorWithRGB(242, 242, 242);
//    [self setupdata];
    [self setupfooter];
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
-(void)shareclickWithIndex:(NSInteger)index{
    NSString *subtitle=WebSharestring;
    
    switch (index) {
        case 0:
        {
            
           
            [UMSocialData defaultData].extConfig.wxMessageType =UMSocialWXMessageTypeWeb;
            [UMSocialData defaultData].extConfig.wechatSessionData.title =@"车镇车源";
            
            [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToWechatSession] content:subtitle image:[UIImage imageNamed:@"logo"] location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response){
                if (response.responseCode == UMSResponseCodeSuccess) {
                    NSLog(@"分享成功！");
                }
            }];
        }
            break;
        case 1:
        {
            
            [UMSocialData defaultData].extConfig.wxMessageType =UMSocialWXMessageTypeWeb;
            [UMSocialData defaultData].extConfig.wechatSessionData.title =@"车镇车源";
            
            [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToWechatTimeline] content:subtitle image:[UIImage imageNamed:@"logo"] location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response){
                if (response.responseCode == UMSResponseCodeSuccess) {
                    NSLog(@"分享成功！");
                }
            }];
        }
            break;
            
        case 2:
        {
            
            
            
        }
            break;
            
        default:
            break;
    }
    

}
-(void)setupfooter{
    UIView *footer=[[UIView alloc]initWithFrame:CGRectMake(0, 0, XHScreenwidth, 84)];
    footer.backgroundColor=[UIColor clearColor];
    UIButton *loginout=[[UIButton alloc]initWithFrame:CGRectMake(16, 24, XHScreenwidth-32, 36)];
    [loginout setBackgroundImage:[UIImage imageWithStretchableName:@"bottom_bg"] forState:UIControlStateNormal];
    [loginout setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginout setTitle:@"退出登录" forState:UIControlStateNormal];
    [loginout addTarget:self action:@selector(loginout) forControlEvents:UIControlEventTouchUpInside];
    [footer addSubview:loginout];
    self.footview=footer;
    self.tableView.tableFooterView=self.footview;
}
-(NSArray *)settingitemarray{
    if (_settingitemarray==nil) {
        SettingItem *updatepass=[[SettingItem alloc]init];
        updatepass.name=@"修改密码";
        updatepass.block=^{
            MYUpatePassViewController *upvc=[[MYUpatePassViewController alloc]initWithNibName:@"MYUpatePassViewController" bundle:nil];
            [self.navigationController pushViewController:upvc animated:YES];
        };
        SettingItem *userguid=[[SettingItem alloc]init];
        userguid.name=@"车镇车源协议";
        userguid.block=^{
            [self pustoWebView:Agreement];
        };
        SettingItem *somequestion=[[SettingItem alloc]init];
        somequestion.name=@"常见问题";
        somequestion.block=^{
          [self pustoWebView:Question];
        };
        SettingItem *shareto=[[SettingItem alloc]init];
        shareto.name=@"分享车镇给朋友";
        shareto.block=^{
            [self share];
        };
        SettingItem *about=[[SettingItem alloc]init];
        
        about.name=@"关于车镇平台";
        about.block=^{
            AboutViewController *vc=[[AboutViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        };
        SettingItem *tell=[[SettingItem alloc]init];
        tell.name=@"客服热线400-822-7177";
        tell.block=^{
            [self tell];
        };
        SettingItem *contact=[[SettingItem alloc]init];
        contact.name=@"联系管理员/意见反馈";
        contact.block=^{
            
            NSLog(@"跳转");
            [self pushtoMessage];
        };
     _settingitemarray=@[updatepass,userguid,somequestion,shareto,about,tell,contact];
    }
    return _settingitemarray;
}
-(void)tell{
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"确定要拨打此电话么" message:@"400-822-7177" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    alert.tag=0;
    [alert show];
}
-(void)pustoWebView:(Webtype)type{
    WebViewController *webvc=[WebViewController ViewController];
    webvc.type=type;
    [self.navigationController pushViewController:webvc animated:YES];
}

-(void)share{
    NSArray *titlearray=@[@"微信",@"朋友圈",@"短信"];
    UIImage *weixin=[UIImage imageNamed:@"weixin"];
    UIImage *pengyou=[UIImage imageNamed:@"liulan"];
    UIImage *sms=[UIImage imageNamed:@"duanxin"];
    NSArray *icon=@[weixin,pengyou,sms];
    ShareView *share=[[ShareView alloc]initWithTitle:titlearray andIcons:icon delegate:self];
    [share show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSLog(@"%ld",buttonIndex);
    if (buttonIndex==0) return;
    if (alertView.tag==0) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://4008227177"]];
    }else{
        self.user=nil;
        AppDelegate *appdelegate=[[UIApplication sharedApplication] delegate];
        [appdelegate saveContext];
        [XHUserDeflaut setBool:NO forKey:LOGINSTATE];
        
        [APService setAlias:@"" callbackSelector:nil object:nil];
        [self.navigationController.navigationController popToRootViewControllerAnimated:YES];
       
        
    }
}

-(void)loginout{
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"确定退出登录么？" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    alert.tag=1;
    [alert show];
    
   
}


//-(void)setupdata{
//    SettingItem *updatepass=[[SettingItem alloc]init];
//    updatepass.name=@"修改密码";
//    
//    SettingItem *userguid=[[SettingItem alloc]init];
//    userguid.name=@"车镇车源协议";
//    
//    SettingItem *somequestion=[[SettingItem alloc]init];
//    somequestion.name=@"常见问题";
//    SettingItem *shareto=[[SettingItem alloc]init];
//    shareto.name=@"分享车镇给朋友";
//    SettingItem *about=[[SettingItem alloc]init];
//    about.name=@"关于车镇平台";
//    SettingItem *tell=[[SettingItem alloc]init];
//    tell.name=@"客服热线400-879-1800";
//    SettingItem *contact=[[SettingItem alloc]init];
//    contact.name=@"联系管理员/意见反馈";
//    contact.block=^{
//
//        NSLog(@"跳转");
//        [self pushtoMessage];
//    };
//    
//    self.settingitemarray=@[updatepass,userguid,somequestion,shareto,about,tell,contact];
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)pushtoMessage{
    MessageDetailTableViewController *messagedetail=[[MessageDetailTableViewController alloc]init];
    messagedetail.navigationItem.title=@"管理员";
    messagedetail.toid=@"28";
    messagedetail.totype=@"4";
    messagedetail.toico=adminico;
    [self.navigationController pushViewController:messagedetail animated:YES];

}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return self.settingitemarray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellid=@"settingcell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.font=[UIFont systemFontOfSize:15];
    }
    // Configure the cell...
    SettingItem *item=self.settingitemarray[indexPath.row];
    cell.textLabel.text=item.name;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SettingItem *item=self.settingitemarray[indexPath.row];
    if (item.block) {
        item.block();
    }
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
